In [1]:
import sympy
import sympy.physics.mechanics as me
import scipy.integrate
import scipy.optimize
%pylab inline

import matplotlib.animation

# video animation
from tempfile import NamedTemporaryFile
from IPython.display import HTML

sympy.init_printing()

Definitions

Define symbols.


In [2]:
t, theta_a, Q_a, theta_b, Q_b, theta_c, Q_c, l_a, l_b, l_c, l_d, I_a, I_b, I_c, m_a, m_b, m_c, g = \
    sympy.symbols(r't, theta_a, Q_a, theta_b, Q_b, theta_c, Q_c, l_a, l_b, l_c, l_d, I_a, I_b, I_c, m_a, m_b, m_c, g')

Define frame and points.


In [3]:
frame_i = me.ReferenceFrame(name='i') # inertial frame
frame_a = frame_i.orientnew(newname='a', rot_type='Axis', amounts=(theta_a(t), frame_i.z)) # link a frame
frame_b = frame_a.orientnew(newname='b', rot_type='Axis', amounts=(theta_b(t), frame_a.z)) # link b frame
frame_c = frame_b.orientnew(newname='c', rot_type='Axis', amounts=(theta_c(t), frame_b.z)) # link b frame

Define bar a.


In [4]:
point_a = me.Point(name='point_a') # axis of rotation for link a
point_a.set_vel(frame=frame_i, value=0) # fixed in inertial frame

cm_a = point_a.locatenew(name='cm_a', value=l_a/2*frame_a.x) # center of mass location
cm_a.v2pt_theory(otherpoint=point_a, outframe=frame_i, fixedframe=frame_a)

bar_a = me.RigidBody(name='bar_a', masscenter=cm_a, frame=frame_a,
                      mass=m_a, inertia=(me.inertia(frame_a, ixx=0, iyy=0, izz=I_a), point_a))

Define bar b.


In [5]:
point_b = point_a.locatenew(name='point_b', value=l_a*frame_a.x) # point b
point_b.v2pt_theory(otherpoint=point_a, outframe=frame_i, fixedframe=frame_b)

cm_b= point_b.locatenew(name='cm_b', value=l_b/2*frame_b.x) # center of mass location
cm_b.v2pt_theory(otherpoint=point_b, outframe=frame_i, fixedframe=frame_b)

bar_b = me.RigidBody(name='bar_b', masscenter=cm_b, frame=frame_b,
                      mass=m_b, inertia=(me.inertia(frame_b, ixx=0, iyy=0, izz=I_b), point_b))

Define bar c.


In [6]:
point_c = point_b.locatenew('point_c', l_b*frame_b.x) # point c
point_c.v2pt_theory(otherpoint=point_b, outframe=frame_i, fixedframe=frame_c)

cm_c= point_c.locatenew('cm_c', l_c/2*frame_c.x) # center of mass location
cm_c.v2pt_theory(otherpoint=point_c, outframe=frame_i, fixedframe=frame_c)

bar_c = me.RigidBody(name='bar_c', masscenter=cm_c, frame=frame_c,
                      mass=m_c, inertia=(me.inertia(frame_c, ixx=0, iyy=0, izz=I_c), point_c))

Define the pinned point at the end of bar c.


In [7]:
point_d = point_c.locatenew('point_d', l_c*frame_c.x) # point d

List of rigid bodies.


In [8]:
rigid_body_list = [bar_a, bar_b, bar_c]

Constraints

Find the constraint equations such that the end point is fixed.


In [9]:
coneq = (point_d.pos_from(point_a) - l_d*frame_i.x).express(frame_i).simplify()
coneq


Out[9]:
$$(l_{a} \operatorname{cos}\left(\theta_{a}\right) + l_{b} \operatorname{cos}\left(\theta_{a} + \theta_{b}\right) + l_{c} \operatorname{cos}\left(\theta_{a} + \theta_{b} + \theta_{c}\right) - l_{d})\mathbf{\hat{i}_x} + (l_{a} \operatorname{sin}\left(\theta_{a}\right) + l_{b} \operatorname{sin}\left(\theta_{a} + \theta_{b}\right) + l_{c} \operatorname{sin}\left(\theta_{a} + \theta_{b} + \theta_{c}\right))\mathbf{\hat{i}_y}$$

Take the derivative of the constraint equations for use in deriving the equations of motion.


In [10]:
coneq_diff = coneq.diff(t, frame_i).express(frame_i).simplify()
coneq_diff


Out[10]:
$$(- l_{a} \operatorname{sin}\left(\theta_{a}\right) \dot{\theta}_{a} - l_{b} \left(\dot{\theta}_{a} + \dot{\theta}_{b}\right) \operatorname{sin}\left(\theta_{a} + \theta_{b}\right) - l_{c} \left(\dot{\theta}_{a} + \dot{\theta}_{b} + \dot{\theta}_{c}\right) \operatorname{sin}\left(\theta_{a} + \theta_{b} + \theta_{c}\right))\mathbf{\hat{i}_x} + (l_{a} \operatorname{cos}\left(\theta_{a}\right) \dot{\theta}_{a} + l_{b} \left(\dot{\theta}_{a} + \dot{\theta}_{b}\right) \operatorname{cos}\left(\theta_{a} + \theta_{b}\right) + l_{c} \left(\dot{\theta}_{a} + \dot{\theta}_{b} + \dot{\theta}_{c}\right) \operatorname{cos}\left(\theta_{a} + \theta_{b} + \theta_{c}\right))\mathbf{\hat{i}_y}$$

In [11]:
coneqs = [coneq.dot(frame_i.x).simplify(), coneq.dot(frame_i.y).simplify()]
coneqs_diff = [coneq_diff.dot(frame_i.x).simplify(), coneq_diff.dot(frame_i.y).simplify()]

Forces and Moments

Define the forces and moments acting on the system.


In [12]:
force_moment_list = [
#(cm_a, -m_a*g*frame_i.y), # weight on a, doing horizontal, so weight is cancelled
#(cm_b, -m_b*g*frame_i.y), # weight on b
#(cm_c, -m_c*g*frame_i.y), # weight on c
(frame_a, (3 - theta_a(t).diff(t))*frame_a.z), # moment from motor, FIXME, these are bogus, 
    # just setting it to something interesting, added a damping term
(point_c, 1*frame_a.y), # lift from wing, FIXME
(frame_c, -1*frame_a.y), # moment from wing, FIXME
]

Equations of Motion

We will not explicitly show the time dependendence for the states.


In [13]:
x_no_t_sub = {theta_a(t): theta_a, theta_b(t): theta_b, theta_c(t): theta_c,
              theta_a(t).diff(t): Q_a, theta_b(t).diff(t): Q_b, theta_c(t).diff(t): Q_c,
              Q_a(t):Q_a, Q_b(t):Q_b, Q_c(t):Q_c}

Lagrange's Method

Find the Lagrangian.


In [14]:
L = me.Lagrangian(frame_i, *rigid_body_list)
L.subs(x_no_t_sub)


Out[14]:
$$\frac{Q_{a}^{2} m_{a}}{8} l_{a}^{2} + \frac{Q_{a}^{2}}{2} \left(I_{a} - \frac{l_{a}^{2} m_{a}}{4}\right) + \frac{Q_{a}}{2} \left(I_{b} - \frac{l_{b}^{2} m_{b}}{4}\right) \left(Q_{a} + Q_{b}\right) + \frac{Q_{a}}{2} \left(I_{c} - \frac{l_{c}^{2} m_{c}}{4}\right) \left(Q_{a} + Q_{b} + Q_{c}\right) + \frac{Q_{b}}{2} \left(I_{b} - \frac{l_{b}^{2} m_{b}}{4}\right) \left(Q_{a} + Q_{b}\right) + \frac{Q_{b}}{2} \left(I_{c} - \frac{l_{c}^{2} m_{c}}{4}\right) \left(Q_{a} + Q_{b} + Q_{c}\right) + \frac{Q_{c}}{2} \left(I_{c} - \frac{l_{c}^{2} m_{c}}{4}\right) \left(Q_{a} + Q_{b} + Q_{c}\right) + \frac{m_{b}}{2} \left(l_{a}^{2} \left(Q_{a} + Q_{b}\right)^{2} + l_{a} l_{b} \left(Q_{a} + Q_{b}\right)^{2} \cos{\left (\theta_{b} \right )} + \frac{l_{b}^{2}}{4} \left(Q_{a} + Q_{b}\right)^{2}\right) + \frac{m_{c}}{2} \left(l_{a}^{2} \left(Q_{a} + Q_{b}\right)^{2} + 2 l_{a} l_{b} \left(Q_{a} + Q_{b}\right) \left(Q_{a} + Q_{b} + Q_{c}\right) \cos{\left (\theta_{b} \right )} + l_{a} l_{c} \left(Q_{a} + Q_{b}\right) \left(- \sin{\left (\theta_{b} \right )} \sin{\left (\theta_{c} \right )} + \cos{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )}\right) \left(Q_{a} + Q_{b} + Q_{c}\right) + l_{b}^{2} \left(Q_{a} + Q_{b} + Q_{c}\right)^{2} + l_{b} l_{c} \left(Q_{a} + Q_{b} + Q_{c}\right)^{2} \cos{\left (\theta_{c} \right )} + \frac{l_{c}^{2}}{4} \left(Q_{a} + Q_{b} + Q_{c}\right)^{2}\right)$$

In [15]:
q_list = [theta_a(t), theta_b(t), theta_c(t)]
lm = me.LagrangesMethod(q_list=q_list, Lagrangian=L, frame=frame_i, coneqs=coneqs_diff, forcelist=force_moment_list)
lm.form_lagranges_equations();

In [16]:
lm_mass_matrix_full = lm.mass_matrix_full.subs(x_no_t_sub)
lm_mass_matrix_full


Out[16]:
$$\left[\begin{matrix}1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\0 & 0 & 0 & I_{a} + I_{b} + I_{c} - \frac{l_{b}^{2} m_{b}}{4} - \frac{l_{c}^{2} m_{c}}{4} + \frac{m_{b}}{2} \left(2 l_{a}^{2} + 2 l_{a} l_{b} \cos{\left (\theta_{b} \right )} + \frac{l_{b}^{2}}{2}\right) + \frac{m_{c}}{2} \left(2 l_{a}^{2} + 4 l_{a} l_{b} \cos{\left (\theta_{b} \right )} + 2 l_{a} l_{c} \left(- \sin{\left (\theta_{b} \right )} \sin{\left (\theta_{c} \right )} + \cos{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )}\right) + 2 l_{b}^{2} + 2 l_{b} l_{c} \cos{\left (\theta_{c} \right )} + \frac{l_{c}^{2}}{2}\right) & I_{b} + I_{c} - \frac{l_{b}^{2} m_{b}}{4} - \frac{l_{c}^{2} m_{c}}{4} + \frac{m_{b}}{2} \left(2 l_{a}^{2} + 2 l_{a} l_{b} \cos{\left (\theta_{b} \right )} + \frac{l_{b}^{2}}{2}\right) + \frac{m_{c}}{2} \left(2 l_{a}^{2} + 4 l_{a} l_{b} \cos{\left (\theta_{b} \right )} + 2 l_{a} l_{c} \left(- \sin{\left (\theta_{b} \right )} \sin{\left (\theta_{c} \right )} + \cos{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )}\right) + 2 l_{b}^{2} + 2 l_{b} l_{c} \cos{\left (\theta_{c} \right )} + \frac{l_{c}^{2}}{2}\right) & I_{c} - \frac{l_{c}^{2} m_{c}}{4} + \frac{m_{c}}{2} \left(2 l_{a} l_{b} \cos{\left (\theta_{b} \right )} + l_{a} l_{c} \left(- \sin{\left (\theta_{b} \right )} \sin{\left (\theta_{c} \right )} + \cos{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )}\right) + 2 l_{b}^{2} + 2 l_{b} l_{c} \cos{\left (\theta_{c} \right )} + \frac{l_{c}^{2}}{2}\right) & l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )} & - l_{a} \cos{\left (\theta_{a} \right )} - l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\\0 & 0 & 0 & I_{b} + I_{c} - \frac{l_{b}^{2} m_{b}}{4} - \frac{l_{c}^{2} m_{c}}{4} + \frac{m_{b}}{2} \left(2 l_{a}^{2} + 2 l_{a} l_{b} \cos{\left (\theta_{b} \right )} + \frac{l_{b}^{2}}{2}\right) + \frac{m_{c}}{2} \left(2 l_{a}^{2} + 4 l_{a} l_{b} \cos{\left (\theta_{b} \right )} + 2 l_{a} l_{c} \left(- \sin{\left (\theta_{b} \right )} \sin{\left (\theta_{c} \right )} + \cos{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )}\right) + 2 l_{b}^{2} + 2 l_{b} l_{c} \cos{\left (\theta_{c} \right )} + \frac{l_{c}^{2}}{2}\right) & I_{b} + I_{c} - \frac{l_{b}^{2} m_{b}}{4} - \frac{l_{c}^{2} m_{c}}{4} + \frac{m_{b}}{2} \left(2 l_{a}^{2} + 2 l_{a} l_{b} \cos{\left (\theta_{b} \right )} + \frac{l_{b}^{2}}{2}\right) + \frac{m_{c}}{2} \left(2 l_{a}^{2} + 4 l_{a} l_{b} \cos{\left (\theta_{b} \right )} + 2 l_{a} l_{c} \left(- \sin{\left (\theta_{b} \right )} \sin{\left (\theta_{c} \right )} + \cos{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )}\right) + 2 l_{b}^{2} + 2 l_{b} l_{c} \cos{\left (\theta_{c} \right )} + \frac{l_{c}^{2}}{2}\right) & I_{c} - \frac{l_{c}^{2} m_{c}}{4} + \frac{m_{c}}{2} \left(2 l_{a} l_{b} \cos{\left (\theta_{b} \right )} + l_{a} l_{c} \left(- \sin{\left (\theta_{b} \right )} \sin{\left (\theta_{c} \right )} + \cos{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )}\right) + 2 l_{b}^{2} + 2 l_{b} l_{c} \cos{\left (\theta_{c} \right )} + \frac{l_{c}^{2}}{2}\right) & l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )} & - l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\\0 & 0 & 0 & I_{c} - \frac{l_{c}^{2} m_{c}}{4} + \frac{m_{c}}{2} \left(2 l_{a} l_{b} \cos{\left (\theta_{b} \right )} + l_{a} l_{c} \left(- \sin{\left (\theta_{b} \right )} \sin{\left (\theta_{c} \right )} + \cos{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )}\right) + 2 l_{b}^{2} + 2 l_{b} l_{c} \cos{\left (\theta_{c} \right )} + \frac{l_{c}^{2}}{2}\right) & I_{c} - \frac{l_{c}^{2} m_{c}}{4} + \frac{m_{c}}{2} \left(2 l_{a} l_{b} \cos{\left (\theta_{b} \right )} + l_{a} l_{c} \left(- \sin{\left (\theta_{b} \right )} \sin{\left (\theta_{c} \right )} + \cos{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )}\right) + 2 l_{b}^{2} + 2 l_{b} l_{c} \cos{\left (\theta_{c} \right )} + \frac{l_{c}^{2}}{2}\right) & I_{c} - \frac{l_{c}^{2} m_{c}}{4} + \frac{m_{c}}{2} \left(2 l_{b}^{2} + 2 l_{b} l_{c} \cos{\left (\theta_{c} \right )} + \frac{l_{c}^{2}}{2}\right) & l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )} & - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\\0 & 0 & 0 & - l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )} & - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )} & - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )} & 0 & 0\\0 & 0 & 0 & l_{a} \cos{\left (\theta_{a} \right )} + l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )} & l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )} & l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )} & 0 & 0\end{matrix}\right]$$

In [17]:
lm_forcing_full = lm.forcing_full.subs(x_no_t_sub)
lm_forcing_full


Out[17]:
$$\left[\begin{matrix}Q_{a}\\Q_{b}\\Q_{c}\\- Q_{a} + \frac{Q_{b} l_{a}}{2} l_{b} m_{b} \left(2 Q_{a} + 2 Q_{b}\right) \sin{\left (\theta_{b} \right )} + l_{a} + l_{b} \cos{\left (\theta_{b} \right )} - \frac{m_{c}}{2} \left(- 2 Q_{b} l_{a} l_{b} \left(Q_{a} + Q_{b}\right) \sin{\left (\theta_{b} \right )} - 2 Q_{b} l_{a} l_{b} \left(Q_{a} + Q_{b} + Q_{c}\right) \sin{\left (\theta_{b} \right )} - Q_{c} l_{b} l_{c} \left(2 Q_{a} + 2 Q_{b} + 2 Q_{c}\right) \sin{\left (\theta_{c} \right )} + l_{a} l_{c} \left(Q_{a} + Q_{b}\right) \left(- Q_{b} \sin{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )} - Q_{b} \sin{\left (\theta_{c} \right )} \cos{\left (\theta_{b} \right )} - Q_{c} \sin{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )} - Q_{c} \sin{\left (\theta_{c} \right )} \cos{\left (\theta_{b} \right )}\right) + l_{a} l_{c} \left(Q_{a} + Q_{b} + Q_{c}\right) \left(- Q_{b} \sin{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )} - Q_{b} \sin{\left (\theta_{c} \right )} \cos{\left (\theta_{b} \right )} - Q_{c} \sin{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )} - Q_{c} \sin{\left (\theta_{c} \right )} \cos{\left (\theta_{b} \right )}\right)\right) + 3\\\frac{Q_{b} l_{a}}{2} l_{b} m_{b} \left(2 Q_{a} + 2 Q_{b}\right) \sin{\left (\theta_{b} \right )} - \frac{l_{a} l_{b}}{2} m_{b} \left(Q_{a} + Q_{b}\right)^{2} \sin{\left (\theta_{b} \right )} + l_{a} + l_{b} \cos{\left (\theta_{b} \right )} + \frac{m_{c}}{2} \left(- 2 l_{a} l_{b} \left(Q_{a} + Q_{b}\right) \left(Q_{a} + Q_{b} + Q_{c}\right) \sin{\left (\theta_{b} \right )} + l_{a} l_{c} \left(Q_{a} + Q_{b}\right) \left(- \sin{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )} - \sin{\left (\theta_{c} \right )} \cos{\left (\theta_{b} \right )}\right) \left(Q_{a} + Q_{b} + Q_{c}\right)\right) - \frac{m_{c}}{2} \left(- 2 Q_{b} l_{a} l_{b} \left(Q_{a} + Q_{b}\right) \sin{\left (\theta_{b} \right )} - 2 Q_{b} l_{a} l_{b} \left(Q_{a} + Q_{b} + Q_{c}\right) \sin{\left (\theta_{b} \right )} - Q_{c} l_{b} l_{c} \left(2 Q_{a} + 2 Q_{b} + 2 Q_{c}\right) \sin{\left (\theta_{c} \right )} + l_{a} l_{c} \left(Q_{a} + Q_{b}\right) \left(- Q_{b} \sin{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )} - Q_{b} \sin{\left (\theta_{c} \right )} \cos{\left (\theta_{b} \right )} - Q_{c} \sin{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )} - Q_{c} \sin{\left (\theta_{c} \right )} \cos{\left (\theta_{b} \right )}\right) + l_{a} l_{c} \left(Q_{a} + Q_{b} + Q_{c}\right) \left(- Q_{b} \sin{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )} - Q_{b} \sin{\left (\theta_{c} \right )} \cos{\left (\theta_{b} \right )} - Q_{c} \sin{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )} - Q_{c} \sin{\left (\theta_{c} \right )} \cos{\left (\theta_{b} \right )}\right)\right)\\l_{b} \cos{\left (\theta_{b} \right )} + \frac{m_{c}}{2} \left(l_{a} l_{c} \left(Q_{a} + Q_{b}\right) \left(- \sin{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )} - \sin{\left (\theta_{c} \right )} \cos{\left (\theta_{b} \right )}\right) \left(Q_{a} + Q_{b} + Q_{c}\right) - l_{b} l_{c} \left(Q_{a} + Q_{b} + Q_{c}\right)^{2} \sin{\left (\theta_{c} \right )}\right) - \frac{m_{c}}{2} \left(- 2 Q_{b} l_{a} l_{b} \left(Q_{a} + Q_{b}\right) \sin{\left (\theta_{b} \right )} - Q_{c} l_{b} l_{c} \left(2 Q_{a} + 2 Q_{b} + 2 Q_{c}\right) \sin{\left (\theta_{c} \right )} + l_{a} l_{c} \left(Q_{a} + Q_{b}\right) \left(- Q_{b} \sin{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )} - Q_{b} \sin{\left (\theta_{c} \right )} \cos{\left (\theta_{b} \right )} - Q_{c} \sin{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )} - Q_{c} \sin{\left (\theta_{c} \right )} \cos{\left (\theta_{b} \right )}\right)\right)\\Q_{a}^{2} l_{a} \cos{\left (\theta_{a} \right )} + l_{b} \left(Q_{a} + Q_{b}\right)^{2} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \left(Q_{a} + Q_{b} + Q_{c}\right)^{2} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\\Q_{a}^{2} l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \left(Q_{a} + Q_{b}\right)^{2} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \left(Q_{a} + Q_{b} + Q_{c}\right)^{2} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\end{matrix}\right]$$

Kane's Method


In [18]:
km = me.KanesMethod(
    frame=frame_i,
    q_ind=[theta_a(t)],
    q_dependent=[theta_b(t), theta_c(t)],
    u_ind=[Q_a(t), Q_b(t), Q_c(t)],
    kd_eqs=[theta_a(t).diff(t) - Q_a(t), theta_b(t).diff(t) - Q_b(t), theta_c(t).diff(t) - Q_c(t)],
    configuration_constraints=coneqs,
)
km.kanes_equations(FL=force_moment_list, BL=rigid_body_list);

In [19]:
km_mass_matrix_full = km.mass_matrix_full.subs(x_no_t_sub)
km_mass_matrix_full


Out[19]:
$$\left[\begin{matrix}1 & 0 & 0 & 0 & 0 & 0\\0 & 1 & 0 & 0 & 0 & 0\\0 & 0 & 1 & 0 & 0 & 0\\0 & 0 & 0 & I_{a} + I_{b} + I_{c} - \frac{l_{b}^{2} m_{b}}{4} - \frac{l_{c}^{2} m_{c}}{4} + m_{b} \left(l_{a}^{2} + l_{a} l_{b} \cos{\left (\theta_{b} \right )} + \frac{l_{b}^{2}}{4}\right) + m_{c} \left(l_{a}^{2} + 2 l_{a} l_{b} \cos{\left (\theta_{b} \right )} + l_{a} l_{c} \left(- \sin{\left (\theta_{b} \right )} \sin{\left (\theta_{c} \right )} + \cos{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )}\right) + l_{b}^{2} + l_{b} l_{c} \cos{\left (\theta_{c} \right )} + \frac{l_{c}^{2}}{4}\right) & I_{b} + I_{c} - \frac{l_{b}^{2} m_{b}}{4} - \frac{l_{c}^{2} m_{c}}{4} + m_{b} \left(l_{a}^{2} + l_{a} l_{b} \cos{\left (\theta_{b} \right )} + \frac{l_{b}^{2}}{4}\right) + m_{c} \left(l_{a}^{2} + 2 l_{a} l_{b} \cos{\left (\theta_{b} \right )} + l_{a} l_{c} \left(- \sin{\left (\theta_{b} \right )} \sin{\left (\theta_{c} \right )} + \cos{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )}\right) + l_{b}^{2} + l_{b} l_{c} \cos{\left (\theta_{c} \right )} + \frac{l_{c}^{2}}{4}\right) & I_{c} - \frac{l_{c}^{2} m_{c}}{4} + m_{c} \left(l_{a} l_{b} \cos{\left (\theta_{b} \right )} + \frac{l_{a} l_{c}}{2} \left(- \sin{\left (\theta_{b} \right )} \sin{\left (\theta_{c} \right )} + \cos{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )}\right) + l_{b}^{2} + l_{b} l_{c} \cos{\left (\theta_{c} \right )} + \frac{l_{c}^{2}}{4}\right)\\0 & 0 & 0 & I_{b} + I_{c} - \frac{l_{b}^{2} m_{b}}{4} - \frac{l_{c}^{2} m_{c}}{4} + m_{b} \left(l_{a}^{2} + l_{a} l_{b} \cos{\left (\theta_{b} \right )} + \frac{l_{b}^{2}}{4}\right) + m_{c} \left(l_{a}^{2} + 2 l_{a} l_{b} \cos{\left (\theta_{b} \right )} + l_{a} l_{c} \left(- \sin{\left (\theta_{b} \right )} \sin{\left (\theta_{c} \right )} + \cos{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )}\right) + l_{b}^{2} + l_{b} l_{c} \cos{\left (\theta_{c} \right )} + \frac{l_{c}^{2}}{4}\right) & I_{b} + I_{c} - \frac{l_{b}^{2} m_{b}}{4} - \frac{l_{c}^{2} m_{c}}{4} + m_{b} \left(l_{a}^{2} + l_{a} l_{b} \cos{\left (\theta_{b} \right )} + \frac{l_{b}^{2}}{4}\right) + m_{c} \left(l_{a}^{2} + 2 l_{a} l_{b} \cos{\left (\theta_{b} \right )} + l_{a} l_{c} \left(- \sin{\left (\theta_{b} \right )} \sin{\left (\theta_{c} \right )} + \cos{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )}\right) + l_{b}^{2} + l_{b} l_{c} \cos{\left (\theta_{c} \right )} + \frac{l_{c}^{2}}{4}\right) & I_{c} - \frac{l_{c}^{2} m_{c}}{4} + m_{c} \left(l_{a} l_{b} \cos{\left (\theta_{b} \right )} + \frac{l_{a} l_{c}}{2} \left(- \sin{\left (\theta_{b} \right )} \sin{\left (\theta_{c} \right )} + \cos{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )}\right) + l_{b}^{2} + l_{b} l_{c} \cos{\left (\theta_{c} \right )} + \frac{l_{c}^{2}}{4}\right)\\0 & 0 & 0 & I_{c} - \frac{l_{c}^{2} m_{c}}{4} + m_{c} \left(l_{a} l_{b} \cos{\left (\theta_{b} \right )} + \frac{l_{a} l_{c}}{2} \left(- \sin{\left (\theta_{b} \right )} \sin{\left (\theta_{c} \right )} + \cos{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )}\right) + l_{b}^{2} + l_{b} l_{c} \cos{\left (\theta_{c} \right )} + \frac{l_{c}^{2}}{4}\right) & I_{c} - \frac{l_{c}^{2} m_{c}}{4} + m_{c} \left(l_{a} l_{b} \cos{\left (\theta_{b} \right )} + \frac{l_{a} l_{c}}{2} \left(- \sin{\left (\theta_{b} \right )} \sin{\left (\theta_{c} \right )} + \cos{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )}\right) + l_{b}^{2} + l_{b} l_{c} \cos{\left (\theta_{c} \right )} + \frac{l_{c}^{2}}{4}\right) & I_{c} - \frac{l_{c}^{2} m_{c}}{4} + m_{c} \left(l_{b}^{2} + l_{b} l_{c} \cos{\left (\theta_{c} \right )} + \frac{l_{c}^{2}}{4}\right)\end{matrix}\right]$$

In [20]:
#(km_mass_matrix_full - lm_mass_matrix_full).applyfunc(lambda expr: expr.expand().simplify())

In [21]:
km_forcing_full = km.forcing_full.subs(x_no_t_sub)
km_forcing_full


Out[21]:
$$\left[\begin{matrix}Q_{a}\\Q_{b}\\Q_{c}\\- \frac{Q_{a} l_{a}}{2} l_{b} m_{b} \left(Q_{a} + Q_{b}\right) \sin{\left (\theta_{b} \right )} - Q_{a} l_{a} l_{b} m_{c} \left(Q_{a} + Q_{b}\right) \sin{\left (\theta_{b} \right )} + \frac{Q_{a} l_{a}}{2} l_{c} m_{c} \left(Q_{a} + Q_{b}\right) \left(- \sin{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )} - \sin{\left (\theta_{c} \right )} \cos{\left (\theta_{b} \right )}\right) - Q_{a} + \frac{l_{a} l_{b}}{2} m_{b} \left(Q_{a} + Q_{b}\right)^{2} \sin{\left (\theta_{b} \right )} + l_{a} l_{b} m_{c} \left(Q_{a} + Q_{b}\right) \left(Q_{a} + Q_{b} + Q_{c}\right) \sin{\left (\theta_{b} \right )} + \frac{l_{a} l_{c}}{2} m_{c} \left(\sin{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )} + \sin{\left (\theta_{c} \right )} \cos{\left (\theta_{b} \right )}\right) \left(Q_{a} + Q_{b} + Q_{c}\right)^{2} + l_{a} - \frac{l_{b} l_{c}}{2} m_{c} \left(Q_{a} + Q_{b}\right) \left(Q_{a} + Q_{b} + Q_{c}\right) \sin{\left (\theta_{c} \right )} + \frac{l_{b} l_{c}}{2} m_{c} \left(Q_{a} + Q_{b} + Q_{c}\right)^{2} \sin{\left (\theta_{c} \right )} + l_{b} \cos{\left (\theta_{b} \right )} + 3\\- \frac{Q_{a} l_{a}}{2} l_{b} m_{b} \left(Q_{a} + Q_{b}\right) \sin{\left (\theta_{b} \right )} - Q_{a} l_{a} l_{b} m_{c} \left(Q_{a} + Q_{b}\right) \sin{\left (\theta_{b} \right )} + \frac{Q_{a} l_{a}}{2} l_{c} m_{c} \left(Q_{a} + Q_{b}\right) \left(- \sin{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )} - \sin{\left (\theta_{c} \right )} \cos{\left (\theta_{b} \right )}\right) + \frac{l_{a} l_{b}}{2} m_{b} \left(Q_{a} + Q_{b}\right)^{2} \sin{\left (\theta_{b} \right )} + l_{a} l_{b} m_{c} \left(Q_{a} + Q_{b}\right) \left(Q_{a} + Q_{b} + Q_{c}\right) \sin{\left (\theta_{b} \right )} + \frac{l_{a} l_{c}}{2} m_{c} \left(\sin{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )} + \sin{\left (\theta_{c} \right )} \cos{\left (\theta_{b} \right )}\right) \left(Q_{a} + Q_{b} + Q_{c}\right)^{2} + l_{a} - \frac{l_{b} l_{c}}{2} m_{c} \left(Q_{a} + Q_{b}\right) \left(Q_{a} + Q_{b} + Q_{c}\right) \sin{\left (\theta_{c} \right )} + \frac{l_{b} l_{c}}{2} m_{c} \left(Q_{a} + Q_{b} + Q_{c}\right)^{2} \sin{\left (\theta_{c} \right )} + l_{b} \cos{\left (\theta_{b} \right )}\\- Q_{a} l_{a} l_{b} m_{c} \left(Q_{a} + Q_{b}\right) \sin{\left (\theta_{b} \right )} + \frac{Q_{a} l_{a}}{2} l_{c} m_{c} \left(Q_{a} + Q_{b}\right) \left(- \sin{\left (\theta_{b} \right )} \cos{\left (\theta_{c} \right )} - \sin{\left (\theta_{c} \right )} \cos{\left (\theta_{b} \right )}\right) - \frac{l_{b} l_{c}}{2} m_{c} \left(Q_{a} + Q_{b}\right) \left(Q_{a} + Q_{b} + Q_{c}\right) \sin{\left (\theta_{c} \right )} + \frac{l_{b} l_{c}}{2} m_{c} \left(Q_{a} + Q_{b} + Q_{c}\right)^{2} \sin{\left (\theta_{c} \right )} + l_{b} \cos{\left (\theta_{b} \right )}\end{matrix}\right]$$

In [22]:
#(km_mass_matrix_full - lm_mass_matrix_full).applyfunc(lambda expr: expr.expand().simplify())

This should be zero, possible error in setup fo Kane's/ Lagrange's method?


In [23]:
#forcing_remainder = (km_forcing_full - lm_forcing_full).applyfunc(lambda expr: expr.expand(trig=True).simplify())
#forcing_remainder

Simulation

Constants for simulation. Link a is attached to a motor, so it has more inertia.


In [24]:
const = {I_a: 1, I_b: 0.01, I_c: 0.01, l_a: 0.1, l_b: 0.5, l_c: 0.2, l_d: 0.5, m_a:1, m_b:1, m_c:1, g: 9.8}

Get equations for point locations.


In [25]:
f_points = []
for point in [point_b, point_c, point_d]:
    r = point.pos_from(point_a).subs(const).express(frame_a).simplify()
    f_points.append(sympy.lambdify((theta_a(t), theta_b(t), theta_c(t)), [r.dot(frame_i.x), r.dot(frame_i.y)]))

Simulate using numerical integration, inverting at each step using the mass and forcing matrices.


In [26]:
x = sympy.DeferredVector('x')
u = sympy.DeferredVector('u')
x_vect = sympy.Matrix([theta_a, theta_b, theta_c, Q_a, Q_b, Q_c])
x_sub = {x_vect[i]: x[i] for i in range(len(x_vect))}
lm_f_mass_matrix = sympy.lambdify((x),  lm_mass_matrix_full.subs(x_sub).subs(const))
lm_f_forcing_matrix = sympy.lambdify((x), lm_forcing_full.subs(x_sub).subs(const))
lm_f_sim = lambda t, x, u: lm_f_mass_matrix(x).I*lm_f_forcing_matrix(x)

In [27]:
km_f_mass_matrix = sympy.lambdify((x),  km_mass_matrix_full.subs(x_sub).subs(const))
km_f_forcing_matrix = sympy.lambdify((x), km_forcing_full.subs(x_sub).subs(const))
km_f_sim = lambda t, x, u: km_f_mass_matrix(x).I*km_f_forcing_matrix(x)

In [28]:
%%timeit
lm_f_sim(0,[1,1,1,1,1,1,1,1],0)


10000 loops, best of 3: 106 µs per loop

In [29]:
%%timeit
km_f_sim(0,[1,1,1,1,1,1,1,1],0)


10000 loops, best of 3: 87.7 µs per loop

Define a function to initialize the 4 bar linkage based on the angle/ rate of the motor.


In [30]:
con_diff = coneq_diff.express(frame_i).simplify().subs(x_no_t_sub)
con_diff.dot(frame_i.x)**2 + con_diff.dot(frame_i.y)**2


Out[30]:
$$\left(- Q_{a} l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \left(Q_{a} + Q_{b}\right) \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \left(Q_{a} + Q_{b} + Q_{c}\right) \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)^{2} + \left(Q_{a} l_{a} \cos{\left (\theta_{a} \right )} + l_{b} \left(Q_{a} + Q_{b}\right) \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \left(Q_{a} + Q_{b} + Q_{c}\right) \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)^{2}$$

In [31]:
def initialize_4bar(theta_a_val, q_a_val, con_eq, coneq_diff):
    con  = coneq.express(frame_i).simplify().subs(x_no_t_sub)
    con_diff = coneq_diff.express(frame_i).simplify().subs(x_no_t_sub)
    constraint_violation_cost = sympy.lambdify(
        (theta_a, theta_b, theta_c, Q_a, Q_b, Q_c), (con.dot(frame_i.x)**2 + con.dot(frame_i.y)**2 +
        con_diff.dot(frame_i.x)**2 + con_diff.dot(frame_i.y)**2).subs(const))
    res = scipy.optimize.minimize(lambda x: constraint_violation_cost(theta_a_val, x[0], x[1], q_a_val, x[2], x[3]), zeros(4))
    return [theta_a_val, res['x'][0], res['x'][1], q_a_val, res['x'][2], res['x'][3], 0, 0]

In [32]:
def simulate(x0, f_sim, tf=10,dt=1.0/20.0):
    sim = scipy.integrate.ode(f_sim)
    sim.set_integrator('dopri5')
    n_y = len(x0)
    sim.set_initial_value(x0)
    n_t = floor(tf/dt)
    data = {'tf': tf, 'dt': dt, 't': zeros(n_t), 'y': zeros((n_t, n_y))}
    i = 0
    while sim.successful() and i < n_t:
        sim.set_f_params(0)
        sim.integrate(sim.t + dt)
        data['t'][i] = sim.t
        data['y'][i,:] = sim.y
        i += 1
        # note, should reset state to satisfy constraint exactly at end of integration, could use
        # minimize as done in init.
    return data

In [33]:
def plot_4bar(f_points, data):
    n_t = len(data['t'])
    a = np.zeros((n_t,2))
    b = np.zeros((n_t,2))
    c = np.zeros((n_t,2))
    d = np.zeros((n_t,2))
    for i in arange(0, n_t):
        theta_a = data['y'][i,0]
        theta_b = data['y'][i,1]
        theta_c = data['y'][i,2]
        b[i,:] = f_points[0](theta_a, theta_b, theta_c)
        c[i,:] = f_points[1](theta_a, theta_b, theta_c)
        d[i,:] = f_points[2](theta_a, theta_b, theta_c)
    h1 = plot([a[:,0], b[:,0]], [a[:,1], b[:,1]], 'k-')
    h2 = plot([b[:,0], c[:,0]], [b[:,1], c[:,1]], 'r-')
    h3 = plot([c[:,0], d[:,0]], [c[:,1], d[:,1]], 'g-')
    legend([h1[0], h2[0], h3[0]], ['ab', 'bc','cd'], loc='best')

Run the simulation and plot the result.


In [34]:
x0 = initialize_4bar(1.0, 0.0, coneq, coneq_diff)
data = simulate(x0, lm_f_sim, tf=10.0)
figure(figsize=(15,5))
plot_4bar(f_points, data)



In [35]:
plt.rcParams['lines.linewidth'] = 2
plt.rcParams['font.size'] = 12
plt.rcParams['animation.writer'] = 'avconv'
plt.rcParams['animation.codec'] = 'libx264'

In [36]:
def anim_to_html(anim):
    plt.close(anim._fig)
    if not hasattr(anim, '_encoded_video'):
        with NamedTemporaryFile(suffix='.mp4') as f:
            anim.save(f.name, fps=20, extra_args=['-vcodec', 'libx264'])
            video = open(f.name, "rb").read()
        anim._encoded_video = video.encode("base64")
    
    return """<video controls>
    <source src="data:video/x-m4v;base64,{0}" type="video/mp4">
     Your browser does not support the video tag.
    </video>""".format(anim._encoded_video)
matplotlib.animation.Animation._repr_html_ = anim_to_html

In [37]:
def anim_4bar(data, fps=20, rate=1.0):
    fig = plt.figure(figsize=(10,5))
    ax = fig.gca()
    n_t = len(data['t'])
    a = np.zeros((n_t,2))
    b = np.zeros((n_t,2))
    c = np.zeros((n_t,2))
    d = np.zeros((n_t,2))
    t = data['t']
    tf = data['tf']
    dt = data['dt']
    if (fps > 1.0/dt):
        raise IOError('Data dt lower than fps, lower fps')
    for i in arange(0, n_t):
        theta_a = data['y'][i,0]
        theta_b = data['y'][i,1]
        theta_c = data['y'][i,2]
        b[i,:] = f_points[0](theta_a, theta_b, theta_c)
        c[i,:] = f_points[1](theta_a, theta_b, theta_c)
        d[i,:] = f_points[2](theta_a, theta_b, theta_c)
    def update(i):
        ax.cla()
        h1 = ax.plot([a[i,0], b[i,0]], [a[i,1], b[i,1]], 'k-')
        h2 = ax.plot([b[i,0], c[i,0]], [b[i,1], c[i,1]], 'r-')
        h3 = ax.plot([c[i,0], d[i,0]], [c[i,1], d[i,1]], 'g-')
        ax.legend([h1[0], h2[0], h3[0]], ['ab', 'bc','cd'], loc=3)
        ax.axis([-max(abs(b[:,0])), max(abs(c[:,0])), -max(abs(c[:,1])), max(abs(c[:,1]))])
        ax.text((a[0,0] + d[0,0])*0.5, 0, 't: {:f}'.format(t[i,]))
    return matplotlib.animation.FuncAnimation(fig, update, frames=int(tf*fps/rate))

In [38]:
anim_4bar(data)


Out[38]:

Simplified Dynamics

If we neglect the mass of the bars, the dynamics are greatly simplified. This would be a valid assumption if the motor inertia and the forces are much larger than the inertial forces/moments of the bars. Here we assume that the motor can be modelled as a rigid body attached at point a.


In [39]:
motor = me.RigidBody('motor', point_a, frame_a, m_a, (me.inertia(frame_a, ixx=0, iyy=0, izz=I_a), point_a))
L_simp = me.Lagrangian(frame_i, motor)
L_simp


Out[39]:
$$\frac{I_{a}}{2} \left(\frac{d}{d t} \theta_{a}{\left (t \right )}\right)^{2}$$

In [40]:
lm_simp = me.LagrangesMethod(
    L_simp, q_list=(theta_a(t), theta_b(t), theta_c(t)),
    coneqs=coneqs_diff, forcelist=force_moment_list, frame=frame_i)
lm_simp.form_lagranges_equations();

In [41]:
lm_simp_mass_matrix_full = lm_simp.mass_matrix_full.subs(x_no_t_sub)
lm_simp_mass_matrix_full


Out[41]:
$$\left[\begin{matrix}1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\0 & 0 & 0 & I_{a} & 0 & 0 & l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )} & - l_{a} \cos{\left (\theta_{a} \right )} - l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\\0 & 0 & 0 & 0 & 0 & 0 & l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )} & - l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\\0 & 0 & 0 & 0 & 0 & 0 & l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )} & - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\\0 & 0 & 0 & - l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )} & - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )} & - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )} & 0 & 0\\0 & 0 & 0 & l_{a} \cos{\left (\theta_{a} \right )} + l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )} & l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )} & l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )} & 0 & 0\end{matrix}\right]$$

In [42]:
lm_simp_forcing_full = lm_simp.forcing_full.subs(x_no_t_sub)
lm_simp_forcing_full


Out[42]:
$$\left[\begin{matrix}Q_{a}\\Q_{b}\\Q_{c}\\- Q_{a} + l_{a} + l_{b} \cos{\left (\theta_{b} \right )} + 3\\l_{a} + l_{b} \cos{\left (\theta_{b} \right )}\\l_{b} \cos{\left (\theta_{b} \right )}\\Q_{a}^{2} l_{a} \cos{\left (\theta_{a} \right )} + l_{b} \left(Q_{a} + Q_{b}\right)^{2} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \left(Q_{a} + Q_{b} + Q_{c}\right)^{2} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\\Q_{a}^{2} l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \left(Q_{a} + Q_{b}\right)^{2} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \left(Q_{a} + Q_{b} + Q_{c}\right)^{2} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\end{matrix}\right]$$

For this simplified model, it is now possible to analytically take the inverse of the mass matrix. This allows dynamic inversion type controllers and also quicker simulation if the simplifying assumptions are valid.


In [43]:
lm_simp_rhs = lm_simp.rhs().subs(x_no_t_sub)
lm_simp_rhs


Out[43]:
$$\left[\begin{matrix}Q_{a}\\Q_{b}\\Q_{c}\\\left(l_{a} + l_{b} \cos{\left (\theta_{b} \right )}\right) \left(\frac{\sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}}{l_{b} \sin{\left (\theta_{c} \right )}} \left(\frac{1}{I_{a}} \left(- l_{a} \cos{\left (\theta_{a} \right )} - l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) - \frac{1}{I_{a} \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)} \left(- l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\right) - \frac{l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}}{I_{a} \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)}\right) - \frac{\cos{\left (\theta_{b} \right )}}{l_{c} \sin{\left (\theta_{c} \right )}} \left(\frac{1}{I_{a}} \left(- l_{a} \cos{\left (\theta_{a} \right )} - l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) - \frac{1}{I_{a} \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)} \left(- l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\right) \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) + \frac{1}{I_{a}} \left(- Q_{a} + l_{a} + l_{b} \cos{\left (\theta_{b} \right )} + 3\right)\\\left(l_{a} + l_{b} \cos{\left (\theta_{b} \right )}\right) \left(- \frac{1}{l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}} \left(- \frac{\sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}}{l_{b} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \sin{\left (\theta_{c} \right )}} \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- \frac{1}{I_{a}} \left(l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \cos{\left (\theta_{a} \right )} + l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) + \frac{1}{I_{a} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)} \left(l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\right) - \frac{1}{I_{a} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)} \left(- l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\right) + \frac{\sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}}{l_{b} \sin{\left (\theta_{c} \right )}} \left(- \frac{1}{l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}} \left(- l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- \frac{\sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}}{l_{b} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \sin{\left (\theta_{c} \right )}} \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- \frac{1}{I_{a}} \left(l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \cos{\left (\theta_{a} \right )} + l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) + \frac{1}{I_{a} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)} \left(l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\right) - \frac{1}{I_{a} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)} \left(- l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\right) - \frac{\sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}}{l_{b} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \sin{\left (\theta_{c} \right )}} \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- \frac{1}{I_{a}} \left(- l_{a} \cos{\left (\theta_{a} \right )} - l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \cos{\left (\theta_{a} \right )} + l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) + \frac{1}{I_{a} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)} \left(l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- l_{a} \cos{\left (\theta_{a} \right )} - l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\right) - \frac{1}{I_{a} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)} \left(- l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- l_{a} \cos{\left (\theta_{a} \right )} - l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\right)\right) + \left(- \frac{\sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}}{l_{b} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \sin{\left (\theta_{c} \right )}} \left(- \frac{1}{I_{a}} \left(l_{a} \cos{\left (\theta_{a} \right )} + l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) + \frac{1}{I_{a} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)} \left(l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\right) \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) - \frac{- l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}}{I_{a} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)}\right) \left(- Q_{a} + l_{a} + l_{b} \cos{\left (\theta_{b} \right )} + 3\right) + \left(\frac{1}{- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}} + \frac{\left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}}{l_{b} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)^{2} \sin{\left (\theta_{c} \right )}}\right) \left(Q_{a}^{2} l_{a} \cos{\left (\theta_{a} \right )} + l_{b} \left(Q_{a} + Q_{b}\right)^{2} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \left(Q_{a} + Q_{b} + Q_{c}\right)^{2} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) - \frac{\cos{\left (\theta_{b} \right )}}{l_{c} \sin{\left (\theta_{c} \right )}} \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- \frac{1}{l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}} \left(- l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- \frac{\sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}}{l_{b} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \sin{\left (\theta_{c} \right )}} \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- \frac{1}{I_{a}} \left(l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \cos{\left (\theta_{a} \right )} + l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) + \frac{1}{I_{a} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)} \left(l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\right) - \frac{1}{I_{a} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)} \left(- l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\right) - \frac{\sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}}{l_{b} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \sin{\left (\theta_{c} \right )}} \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- \frac{1}{I_{a}} \left(- l_{a} \cos{\left (\theta_{a} \right )} - l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \cos{\left (\theta_{a} \right )} + l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) + \frac{1}{I_{a} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)} \left(l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- l_{a} \cos{\left (\theta_{a} \right )} - l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\right) - \frac{1}{I_{a} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)} \left(- l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- l_{a} \cos{\left (\theta_{a} \right )} - l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\right) - \frac{\sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}}{l_{b} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \sin{\left (\theta_{c} \right )}} \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(Q_{a}^{2} l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \left(Q_{a} + Q_{b}\right)^{2} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \left(Q_{a} + Q_{b} + Q_{c}\right)^{2} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\\\left(l_{a} + l_{b} \cos{\left (\theta_{b} \right )}\right) \left(\frac{\sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}}{l_{b} \sin{\left (\theta_{c} \right )}} \left(- \frac{1}{l_{b} l_{c} \sin{\left (\theta_{c} \right )}} \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- \frac{1}{I_{a}} \left(- l_{a} \cos{\left (\theta_{a} \right )} - l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \cos{\left (\theta_{a} \right )} + l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) + \frac{1}{I_{a} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)} \left(l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- l_{a} \cos{\left (\theta_{a} \right )} - l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\right) + \frac{1}{l_{b} l_{c} \sin{\left (\theta_{c} \right )}} \left(- l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- \frac{1}{I_{a}} \left(l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \cos{\left (\theta_{a} \right )} + l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) + \frac{1}{I_{a} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)} \left(l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\right)\right) + \frac{1}{l_{b} l_{c} \sin{\left (\theta_{c} \right )}} \left(- \frac{1}{I_{a}} \left(l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \cos{\left (\theta_{a} \right )} + l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) + \frac{1}{I_{a} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)} \left(l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\right)\right) - \frac{\cos{\left (\theta_{b} \right )}}{l_{c} \sin{\left (\theta_{c} \right )}} \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- \frac{1}{l_{b} l_{c} \sin{\left (\theta_{c} \right )}} \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- \frac{1}{I_{a}} \left(- l_{a} \cos{\left (\theta_{a} \right )} - l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \cos{\left (\theta_{a} \right )} + l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) + \frac{1}{I_{a} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)} \left(l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- l_{a} \cos{\left (\theta_{a} \right )} - l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\right) + \frac{1}{l_{b} l_{c} \sin{\left (\theta_{c} \right )}} \left(- l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- \frac{1}{I_{a}} \left(l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \cos{\left (\theta_{a} \right )} + l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) + \frac{1}{I_{a} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)} \left(l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\right)\right) - \frac{1}{l_{b} l_{c} \sin{\left (\theta_{c} \right )}} \left(- \frac{1}{I_{a}} \left(l_{a} \cos{\left (\theta_{a} \right )} + l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) + \frac{1}{I_{a} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)} \left(l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- l_{a} \sin{\left (\theta_{a} \right )} - l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\right) \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(- Q_{a} + l_{a} + l_{b} \cos{\left (\theta_{b} \right )} + 3\right) - \frac{1}{l_{b} l_{c} \sin{\left (\theta_{c} \right )}} \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(Q_{a}^{2} l_{a} \sin{\left (\theta_{a} \right )} + l_{b} \left(Q_{a} + Q_{b}\right)^{2} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \left(Q_{a} + Q_{b} + Q_{c}\right)^{2} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) + \frac{1}{l_{b} l_{c} \left(- l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \sin{\left (\theta_{c} \right )}} \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \left(Q_{a}^{2} l_{a} \cos{\left (\theta_{a} \right )} + l_{b} \left(Q_{a} + Q_{b}\right)^{2} \cos{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \left(Q_{a} + Q_{b} + Q_{c}\right)^{2} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\\\left(l_{a} + l_{b} \cos{\left (\theta_{b} \right )}\right) \left(\frac{1}{l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}} + \frac{\left(- l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}}{l_{b} \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) \sin{\left (\theta_{c} \right )}}\right) - \frac{\cos{\left (\theta_{b} \right )}}{l_{c} \sin{\left (\theta_{c} \right )}} \left(- l_{b} \cos{\left (\theta_{a} + \theta_{b} \right )} - l_{c} \cos{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right)\\\frac{\cos{\left (\theta_{b} \right )}}{l_{c} \sin{\left (\theta_{c} \right )}} \left(l_{b} \sin{\left (\theta_{a} + \theta_{b} \right )} + l_{c} \sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}\right) - \frac{\sin{\left (\theta_{a} + \theta_{b} + \theta_{c} \right )}}{l_{b} \sin{\left (\theta_{c} \right )}} \left(l_{a} + l_{b} \cos{\left (\theta_{b} \right )}\right)\end{matrix}\right]$$

In [44]:
lm_simp_f = sympy.lambdify(x, lm_simp_rhs.subs(x_no_t_sub).subs(x_sub).subs(const))
lm_simp_f_sim = lambda t, x, u: lm_simp_f(x)

In [45]:
%%timeit
lm_simp_f_sim(0,[1,1,1,1,1,1,1,1],0)


10000 loops, best of 3: 96.4 µs per loop

In [46]:
x0 = initialize_4bar(1.0, 0.0, coneq, coneq_diff)
data_simp = simulate(x0, lm_simp_f_sim, tf=10.0)
figure(figsize=(15,5))
plot_4bar(f_points, data_simp)



In [47]:
anim_4bar(data_simp)


Out[47]:

In [48]:
figure(figsize(15,5))
h1 = plot(data_simp['t'], data_simp['y'][:,2])
h2 = plot(data['t'], data['y'][:,2])
legend([h1[0], h2[0]], ['simplified', 'complex'])


Out[48]:
<matplotlib.legend.Legend at 0x59b9310>

Code Generation


In [49]:
def generate_matlab(expr):
    return str(expr.subs(x_no_t_sub)).replace('**','^').replace(r'Matrix([[','[').\
    replace(r'], [',';\n').replace(']])','];').replace(r'\dot{\theta}','dtheta')

In [50]:
print generate_matlab(lm.mass_matrix_full)
print generate_matlab(lm.forcing_full)


[1, 0, 0, 0, 0, 0, 0, 0;
0, 1, 0, 0, 0, 0, 0, 0;
0, 0, 1, 0, 0, 0, 0, 0;
0, 0, 0, I_a + I_b + I_c - l_b^2*m_b/4 - l_c^2*m_c/4 + m_b*(2*l_a^2 + 2*l_a*l_b*cos(theta_b) + l_b^2/2)/2 + m_c*(2*l_a^2 + 4*l_a*l_b*cos(theta_b) + 2*l_a*l_c*(-sin(theta_b)*sin(theta_c) + cos(theta_b)*cos(theta_c)) + 2*l_b^2 + 2*l_b*l_c*cos(theta_c) + l_c^2/2)/2, I_b + I_c - l_b^2*m_b/4 - l_c^2*m_c/4 + m_b*(2*l_a^2 + 2*l_a*l_b*cos(theta_b) + l_b^2/2)/2 + m_c*(2*l_a^2 + 4*l_a*l_b*cos(theta_b) + 2*l_a*l_c*(-sin(theta_b)*sin(theta_c) + cos(theta_b)*cos(theta_c)) + 2*l_b^2 + 2*l_b*l_c*cos(theta_c) + l_c^2/2)/2, I_c - l_c^2*m_c/4 + m_c*(2*l_a*l_b*cos(theta_b) + l_a*l_c*(-sin(theta_b)*sin(theta_c) + cos(theta_b)*cos(theta_c)) + 2*l_b^2 + 2*l_b*l_c*cos(theta_c) + l_c^2/2)/2, l_a*sin(theta_a) + l_b*sin(theta_a + theta_b) + l_c*sin(theta_a + theta_b + theta_c), -l_a*cos(theta_a) - l_b*cos(theta_a + theta_b) - l_c*cos(theta_a + theta_b + theta_c);
0, 0, 0, I_b + I_c - l_b^2*m_b/4 - l_c^2*m_c/4 + m_b*(2*l_a^2 + 2*l_a*l_b*cos(theta_b) + l_b^2/2)/2 + m_c*(2*l_a^2 + 4*l_a*l_b*cos(theta_b) + 2*l_a*l_c*(-sin(theta_b)*sin(theta_c) + cos(theta_b)*cos(theta_c)) + 2*l_b^2 + 2*l_b*l_c*cos(theta_c) + l_c^2/2)/2, I_b + I_c - l_b^2*m_b/4 - l_c^2*m_c/4 + m_b*(2*l_a^2 + 2*l_a*l_b*cos(theta_b) + l_b^2/2)/2 + m_c*(2*l_a^2 + 4*l_a*l_b*cos(theta_b) + 2*l_a*l_c*(-sin(theta_b)*sin(theta_c) + cos(theta_b)*cos(theta_c)) + 2*l_b^2 + 2*l_b*l_c*cos(theta_c) + l_c^2/2)/2, I_c - l_c^2*m_c/4 + m_c*(2*l_a*l_b*cos(theta_b) + l_a*l_c*(-sin(theta_b)*sin(theta_c) + cos(theta_b)*cos(theta_c)) + 2*l_b^2 + 2*l_b*l_c*cos(theta_c) + l_c^2/2)/2, l_b*sin(theta_a + theta_b) + l_c*sin(theta_a + theta_b + theta_c), -l_b*cos(theta_a + theta_b) - l_c*cos(theta_a + theta_b + theta_c);
0, 0, 0, I_c - l_c^2*m_c/4 + m_c*(2*l_a*l_b*cos(theta_b) + l_a*l_c*(-sin(theta_b)*sin(theta_c) + cos(theta_b)*cos(theta_c)) + 2*l_b^2 + 2*l_b*l_c*cos(theta_c) + l_c^2/2)/2, I_c - l_c^2*m_c/4 + m_c*(2*l_a*l_b*cos(theta_b) + l_a*l_c*(-sin(theta_b)*sin(theta_c) + cos(theta_b)*cos(theta_c)) + 2*l_b^2 + 2*l_b*l_c*cos(theta_c) + l_c^2/2)/2, I_c - l_c^2*m_c/4 + m_c*(2*l_b^2 + 2*l_b*l_c*cos(theta_c) + l_c^2/2)/2, l_c*sin(theta_a + theta_b + theta_c), -l_c*cos(theta_a + theta_b + theta_c);
0, 0, 0, -l_a*sin(theta_a) - l_b*sin(theta_a + theta_b) - l_c*sin(theta_a + theta_b + theta_c), -l_b*sin(theta_a + theta_b) - l_c*sin(theta_a + theta_b + theta_c), -l_c*sin(theta_a + theta_b + theta_c), 0, 0;
0, 0, 0, l_a*cos(theta_a) + l_b*cos(theta_a + theta_b) + l_c*cos(theta_a + theta_b + theta_c), l_b*cos(theta_a + theta_b) + l_c*cos(theta_a + theta_b + theta_c), l_c*cos(theta_a + theta_b + theta_c), 0, 0];
[Q_a;
Q_b;
Q_c;
-Q_a + Q_b*l_a*l_b*m_b*(2*Q_a + 2*Q_b)*sin(theta_b)/2 + l_a + l_b*cos(theta_b) - m_c*(-2*Q_b*l_a*l_b*(Q_a + Q_b)*sin(theta_b) - 2*Q_b*l_a*l_b*(Q_a + Q_b + Q_c)*sin(theta_b) - Q_c*l_b*l_c*(2*Q_a + 2*Q_b + 2*Q_c)*sin(theta_c) + l_a*l_c*(Q_a + Q_b)*(-Q_b*sin(theta_b)*cos(theta_c) - Q_b*sin(theta_c)*cos(theta_b) - Q_c*sin(theta_b)*cos(theta_c) - Q_c*sin(theta_c)*cos(theta_b)) + l_a*l_c*(Q_a + Q_b + Q_c)*(-Q_b*sin(theta_b)*cos(theta_c) - Q_b*sin(theta_c)*cos(theta_b) - Q_c*sin(theta_b)*cos(theta_c) - Q_c*sin(theta_c)*cos(theta_b)))/2 + 3;
Q_b*l_a*l_b*m_b*(2*Q_a + 2*Q_b)*sin(theta_b)/2 - l_a*l_b*m_b*(Q_a + Q_b)^2*sin(theta_b)/2 + l_a + l_b*cos(theta_b) + m_c*(-2*l_a*l_b*(Q_a + Q_b)*(Q_a + Q_b + Q_c)*sin(theta_b) + l_a*l_c*(Q_a + Q_b)*(-sin(theta_b)*cos(theta_c) - sin(theta_c)*cos(theta_b))*(Q_a + Q_b + Q_c))/2 - m_c*(-2*Q_b*l_a*l_b*(Q_a + Q_b)*sin(theta_b) - 2*Q_b*l_a*l_b*(Q_a + Q_b + Q_c)*sin(theta_b) - Q_c*l_b*l_c*(2*Q_a + 2*Q_b + 2*Q_c)*sin(theta_c) + l_a*l_c*(Q_a + Q_b)*(-Q_b*sin(theta_b)*cos(theta_c) - Q_b*sin(theta_c)*cos(theta_b) - Q_c*sin(theta_b)*cos(theta_c) - Q_c*sin(theta_c)*cos(theta_b)) + l_a*l_c*(Q_a + Q_b + Q_c)*(-Q_b*sin(theta_b)*cos(theta_c) - Q_b*sin(theta_c)*cos(theta_b) - Q_c*sin(theta_b)*cos(theta_c) - Q_c*sin(theta_c)*cos(theta_b)))/2;
l_b*cos(theta_b) + m_c*(l_a*l_c*(Q_a + Q_b)*(-sin(theta_b)*cos(theta_c) - sin(theta_c)*cos(theta_b))*(Q_a + Q_b + Q_c) - l_b*l_c*(Q_a + Q_b + Q_c)^2*sin(theta_c))/2 - m_c*(-2*Q_b*l_a*l_b*(Q_a + Q_b)*sin(theta_b) - Q_c*l_b*l_c*(2*Q_a + 2*Q_b + 2*Q_c)*sin(theta_c) + l_a*l_c*(Q_a + Q_b)*(-Q_b*sin(theta_b)*cos(theta_c) - Q_b*sin(theta_c)*cos(theta_b) - Q_c*sin(theta_b)*cos(theta_c) - Q_c*sin(theta_c)*cos(theta_b)))/2;
Q_a^2*l_a*cos(theta_a) + l_b*(Q_a + Q_b)^2*cos(theta_a + theta_b) + l_c*(Q_a + Q_b + Q_c)^2*cos(theta_a + theta_b + theta_c);
Q_a^2*l_a*sin(theta_a) + l_b*(Q_a + Q_b)^2*sin(theta_a + theta_b) + l_c*(Q_a + Q_b + Q_c)^2*sin(theta_a + theta_b + theta_c)];